package xyz.xtgl.config.helper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * @author liubo
 * @created 2020-02-03 21:51
 */
@Slf4j
public class PageConverter implements Converter<String, Page<?>> {

    @Override
    public Page<?> convert(String source) {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        Page<?> page = new Page<>();
        String limit = request.getParameter("limit");
        String pageSize = request.getParameter("page");
        if(CommonUtil.isNotEmpty(pageSize)){
            page.setCurrent(Long.valueOf(pageSize));
        }
        if(CommonUtil.isNotEmpty(limit)){
            page.setSize(Long.valueOf(limit));
        }
        return page;
    }
}
